#!/bin/bash

cp {{ gaussdb_data_path }}/postgresql.conf  {{ gaussdb_data_path }}/postgresql.conf.bak.`date +"%Y%m%d%H%M%S"`

function SecureAccess(){
    gs_guc set -I all -N all -c "listen_addresses = '*'"
    gs_guc set -I all -N all -c "password_encryption_type=1"
    gs_guc set -I all -N all -c "password_reuse_time=60"
    gs_guc set -I all -N all -c "password_lock_time=1"
    gs_guc set -I all -N all -c "password_effect_time=999"
    gs_guc set -I all -N all -c "session_timeout='0'"
}

function WAL(){
    gs_guc set -I all -N all -c "wal_level=hot_standby"
    gs_guc set -I all -N all -c "full_page_writes=off"
    gs_guc set -I all -N all -c "wal_log_hints=off"
    gs_guc set -I all -N all -c "xloginsert_locks=48"
    gs_guc set -I all -N all -c "advance_xlog_file_num=10"
    gs_guc set -N all -I all -c "archive_mode=on"
    gs_guc set -N all -I all -c "archive_dest='/archive'"
}

function replicationAndSlots(){
    gs_guc set -I all -N all -c "wal_keep_segments=1024"
    gs_guc set -I all -N all -c "max_wal_senders=16"
    gs_guc set -I all -N all -c "most_available_sync=on"
    gs_guc set -I all -N all -c "catchup2normal_wait_time=0"
    gs_guc set -I all -N all -c "enable_slot_log=on"
    gs_guc set -I all -N all -c "max_replication_slots=32"
    gs_guc set -I all -N all -c "wal_receiver_timeout=60s"
    gs_guc set -I all -N all -c "sync_config_strategy=none_node"
    gs_guc set -I all -N all -c "wal_sender_timeout=10s"
}

function dbLog(){
    gs_guc set -I all -N all -c "log_duration=off"
    gs_guc set -I all -N all -c "log_line_prefix='%m %u %d %r %p %S'"
    gs_guc set -I all -N all -c "log_checkpoints=on"
    gs_guc set -I all -N all -c "temp_file_limit=100GB"
}

function VACUUM(){
    gs_guc set -I all -N all -c "vacuum_cost_limit=1000"
    gs_guc set -I all -N all -c "autovacuum_max_workers=10"
    gs_guc set -I all -N all -c "autovacuum_naptime=20s"
    gs_guc set -I all -N all -c "autovacuum_vacuum_cost_delay=10"
    gs_guc set -I all -N all -c "autovacuum_vacuum_scale_factor=0.05"
    gs_guc set -I all -N all -c "autovacuum_analyze_scale_factor=0.02"
    gs_guc set -I all -N all -c "autovacuum_vacuum_threshold=200"
    gs_guc set -I all -N all -c "autovacuum_analyze_threshold=200"
    gs_guc set -I all -N all -c "autovacuum_io_limits=104857600"
}

function perfStats(){
    gs_guc set -I all -N all -c "instr_unique_sql_count=200000"
    gs_guc set -I all -N all -c "enable_wdr_snapshot=on"
    gs_guc set -I all -N all -c "log_min_duration_statement=200"
    gs_guc set -I all -N all -c "track_activity_query_size=2048"
    gs_guc set -I all -N all -c "enable_instr_rt_percentile=off"
    gs_guc set -I all -N all -c "enable_opfusion=off"
}

function audit(){
    gs_guc set -N all -I all -c "audit_enabled=on"
    gs_guc set -N all -I all -c "audit_login_logout=7"
    gs_guc set -N all -I all -c "audit_grant_revoke=1"
    gs_guc set -N all -I all -c "audit_database_process=0"
    gs_guc set -N all -I all -c "audit_user_locked=0"
    gs_guc set -N all -I all -c "audit_user_violation=0"
    gs_guc set -N all -I all -c "audit_system_object=0"
    gs_guc set -N all -I all -c "audit_dml_state_select=0"
    gs_guc set -N all -I all -c "audit_dml_state=0"
    gs_guc set -N all -I all -c "audit_function_exec=0"
    gs_guc set -N all -I all -c "audit_copy_exec=0"
    gs_guc set -N all -I all -c "audit_set_parameter=0"
    gs_guc set -N all -I all -c "audit_xid_info=0"
    gs_guc set -N all -I all -c "audit_directory='/database/panweidb/pg_audit'"
    gs_guc set -N all -I all -c "audit_resource_policy=off"
    gs_guc set -N all -I all -c "audit_file_remain_time=7"
    gs_guc set -N all -I all -c "audit_space_limit=5GB"
}

function otherKeyParams(){
    gs_guc set -I all -N all -c "cstore_buffers=16MB"
    gs_guc set -I all -N all -c "local_syscache_threshold=32MB"
    gs_guc set -I all -N all -c "standby_shared_buffers_fraction=1"
    gs_guc set -I all -N all -c "checkpoint_segments=1024"
    gs_guc set -I all -N all -c "checkpoint_completion_target=0.8"
    gs_guc set -I all -N all -c "max_files_per_process=100000"
    gs_guc set -I all -N all -c "behavior_compat_options='display_leading_zero'"
    gs_guc set -I all -N all -c "lc_messages='en_US.UTF-8'"
    gs_guc set -I all -N all -c "lc_monetary='en_US.UTF-8'"
    gs_guc set -I all -N all -c "lc_numeric='en_US.UTF-8'"
    gs_guc set -I all -N all -c "lc_time='en_US.UTF-8'"
    gs_guc set -N all -I all -c "enable_save_datachanged_timestamp=off"
    gs_guc set -N all -I all -c "track_sql_count=off"
    gs_guc set -N all -I all -c "enable_instr_rt_percentile=off"
    gs_guc set -N all -I all -c "enable_instance_metric_persistent=off"
    gs_guc set -N all -I all -c "enable_logical_io_statistics=off"
    gs_guc set -N all -I all -c "enable_user_metric_persistent=off"
    gs_guc set -N all -I all -c "enable_mergejoin=on"
    gs_guc set -N all -I all -c "enable_nestloop=on"
    gs_guc set -N all -I all -c "enable_pbe_optimization=off"
    gs_guc set -N all -I all -c "enable_asp=on"
    gs_guc set -N all -I all -c "recovery_max_workers=4"
    gs_guc set -N all -I all -c "enable_alarm=off"
    gs_guc set -N all -I all -c "enable_codegen=off"
    gs_guc set -N all -I all -c "pagewriter_sleep=200"
    gs_guc set -N all -I all -c "update_lockwait_timeout=1min"
    gs_guc set -N all -I all -c "lockwait_timeout=1min"
    gs_guc set -N all -I all -c "max_size_for_xlog_prune=104857600"
    gs_guc set -N all -I all -c "gs_clean_timeout=0"
    gs_guc set -N all -I all -c "enable_ustore=off"
    gs_guc set -N all -I all -c "random_page_cost=1.5"
}

memory=`free -g|awk '{print $2}' |sed -n 2p`
if [[ $memory -lt 3 ]]
then
    echo "Defined values of specify parameters or use the default parameters"
else
    if [[ $memory -ge 3 ]] && [[ $memory -le 16 ]] 
    then
        gs_guc set -I all -N all -c "max_process_memory=16GB"
        gs_guc set -I all -N all -c "shared_buffers=$((memory*2/10))GB"
        gs_guc set -I all -N all -c "max_connections=500"
        gs_guc set -I all -N all -c "work_mem=16MB"
        gs_guc set -I all -N all -c "maintenance_work_mem=512MB"
        gs_guc set -I all -N all -c "wal_buffers=128MB"
        gs_guc set -I all -N all -c "max_prepared_transactions=500"
    elif [[ $memory -gt 16 ]] && [[ $memory -le 64 ]] 
    then
        gs_guc set -I all -N all -c "max_process_memory=$((memory*7/10))GB"
        gs_guc set -I all -N all -c "shared_buffers=$((memory*2/10))GB"
        gs_guc set -I all -N all -c "max_connections=1000"
        gs_guc set -I all -N all -c "work_mem=32MB"
        gs_guc set -I all -N all -c "maintenance_work_mem=1GB"
        gs_guc set -I all -N all -c "wal_buffers=512MB"
        gs_guc set -I all -N all -c "max_prepared_transactions=1000"
    elif [[ $memory -gt 64 ]]
    then
        gs_guc set -I all -N all -c "max_process_memory=$((memory*8/10))GB"
        gs_guc set -I all -N all -c "shared_buffers=$((memory*3/10))GB"
        gs_guc set -I all -N all -c "max_connections=3000"
        gs_guc set -I all -N all -c "work_mem=64MB"
        gs_guc set -I all -N all -c "maintenance_work_mem=2GB"
        gs_guc set -I all -N all -c "wal_buffers=1GB"
        gs_guc set -I all -N all -c "max_prepared_transactions=3000"
    else
        echo "There may be a problem with the script, please contact us for support"
    fi
    WAL
    replicationAndSlots
    dbLog
    VACUUM
    perfStats
    otherKeyParams
    audit
fi

SecureAccess
